<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="form" size="small" label-width="76px">
        <el-form-item label="报修类型">
          <el-input v-model="form.repairType" clearable placeholder="请输入报修类型"></el-input>
        </el-form-item>
        <el-form-item label="主管人员">
          <el-select v-model="form.mainMgr" clearable placeholder="请选择主管人员">
            <el-option
              v-for="item in mgrList"
              :key="item.userNo"
              :label="item.userName"
              :value="item.userName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="margin-left: 10px;">查 询</el-button>
          <el-button type="primary" @click="handleAdd" style="margin-left: 10px;">新 增</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="table-card">
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column label="序号" type="index" width="50"> </el-table-column>
        <template v-for="(item, index) in columnsList">
          <el-table-column
            :key="index"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :formatter="item.formatter"
          ></el-table-column>
        </template>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click.native.prevent="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        layout="total,prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <type-modal ref="TypeModal" @ok="modalOk" :mgrList="mgrList" :tableTypeList="tableTypeList"></type-modal>
  </section>
</template>

<script>
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import TypeModal from './modules/TypeModal'
import { getTypeUser, getTypeList, deleteType, getWkTypeList } from './js/repair'
export default {
  components: { TypeModal },
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      form: {
        repairType: '',
        mainMgr: ''
      },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalPageNum: 0,
      mgrList: [],
      tableTypeList: [],
      columnsList: [
        {
          prop: 'name',
          label: '报修类型'
        },
        {
          prop: 'userName',
          label: '主管人员'
        },
        {
          prop: 'mobile',
          label: '主管电话'
        },
        {
          prop: 'status',
          label: '是否启用',
          formatter: (row, col, val) => (val === 100 ? '启用' : '禁用')
        },
        {
          prop: 'createdUser',
          label: '创建人'
        },
        {
          prop: 'workOrderTypeName',
          label: '类型名称'
        },

        {
          prop: 'gmtCreated',
          label: '创建时间'
        },
        {
          prop: 'remark',
          label: '备注'
        }
      ]
    }
  },
  mounted () {
    this.getTypeUser()
    this.getTypeList()
    this.getWkTypeList()
  },
  methods: {
    async getTypeUser () {
      let res = await getTypeUser()
      if (res.code === 0) {
        this.mgrList = res.data
      }
    },
    async getTypeList () {
      let res = await getTypeList({
        name: this.form.repairType,
        userName: this.form.mainMgr,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      })
      if (res.code === 0) {
        this.tableData = res.data.list
        this.totalPageNum = res.data.total
      }
    },
    async getWkTypeList () {
      let res = await getWkTypeList(this.tenantNo)
      if (res.code === 0) {
        this.tableTypeList = res.data
      }
    },
    onSubmit () {
      this.currentPage = 1
      this.getTypeList()
    },
    // 新增按钮
    handleAdd () {
      this.$refs.TypeModal.title = '报修类型新增'
      this.$refs.TypeModal.isAdd = true
      this.$refs.TypeModal.add()
    },
    // 编辑按钮
    handleEdit (row) {
      this.$refs.TypeModal.title = '报修类型编辑'
      this.$refs.TypeModal.isAdd = false
      this.$refs.TypeModal.edit(row)
    },
    // 删除按钮
    handleDelete (row) {
      this.$confirm('此操作将永久删除该报修类型，是否继续执行？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteType(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async deleteType (row) {
      let res = await deleteType(row.id)
      if (res.code === 0) {
        this.$message.success('删除成功')
      } else {
        this.$message.warning(res.msg)
      }
      await this.getTypeList()
    },
    pageHandleChange (page) {
      this.currentPage = page
      this.getTypeList()
    },
    modalOk () {
      this.getTypeList()
    }
  }
}
</script>

<style scoped></style>
